'use strict';
const Controller = require('egg').Controller;
const jwt = require("jsonwebtoken")
class UserController extends Controller {
    //注册
    async registry(ctx) {
        let { email, password, picture } = this.ctx.request.body;
        console.log(email, password, picture);
        let data = await ctx.service.user.registry({ email, password, picture });
        if (data.list) {
            ctx.body = {
                code: 200,
                msg: data.msg
            }
        }
        else {
            ctx.body = {
                code: 404,
                msg: data.msg,
            }
        }

    }
    //登录 
    async login(ctx) {
        //引入bao
        const {file}=this.ctx.request.files;
        console.log(file);
        let { email, password } = this.ctx.request.body;
        console.log(email, password,  file);
        let data = await ctx.service.user.login({ email, password,  file });
        if (data.code === 200) {
            //生成token
            const token = jwt.sign({ email:email, password:password, picture: file }, 'SHA256' , {expiresIn:'10h'});
            // console.log(token);
            //解析密钥
            // jwt.verify(token, 'SHA256');

            ctx.body = {
                code: data.code,
                msg: data.msg,
                token: token,

            }
        }
        else {
            ctx.body = {
                code: data.code,
                msg: data.msg,
            }
        }
    }
    async updataFaceImage(ctx){
        let {files}=ctx.body.request.file[0];
        console.log(files);
        ctx.body={
            code:200,
            msg:"上传成功"
        }

    }
    async getoken(ctx) {
        const token = ctx.get("token");
        console.log(token);
        if (token) {
            try {
                const data = jwt.verify(token, 'SHA256');
                ctx.body = {
                    code: 200,
                    msg: "获取成功",
                    data: data,
                }
            }
            catch {
                ctx.body = {
                    code: 500,
                    msg: "错误",
                }
            }
        }
        else
        {
            ctx.body = {
                code: 404,
                msg: "没有权限",
            }
        }
    }

}

module.exports = UserController;
